package jvm;
/*
*
* 反证java GC不使用 引用计数算法来标记垃圾
* 测试finalize 方法
* */
public class TestGcWay {
    public int[]arrays=new int[1024*1024*10];//10M
    public Object reference;
    public static Object root;//作为可达性分析算法中的根节点 roots
    public static void main(String[] args) {
          test2();
    }
    public static void test1(){
        TestGcWay testGcWay1=new TestGcWay();
        TestGcWay testGcWay2=new TestGcWay();
        testGcWay1.reference=testGcWay2;
        testGcWay2.reference=testGcWay1;

        testGcWay1=null;
        testGcWay2=null;
        System.gc();
    }
    public static void test2(){
        root = new TestGcWay();
        root=null;
        System.out.println("GC...");
        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(root==null) System.out.println("dead...");
        else System.out.println("alive");
        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(root==null) System.out.println("dead...");
        else System.out.println("alive");


    }
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize");
        root=this;//让当前对象与根节点联系
    }
}
